import numpy as np
import matplotlib.pyplot as plt
from sys import argv

if __name__ == "__main__":
    sampling_num = 30
    boundary = 320
    data_r = np.loadtxt("data_robust.txt", delimiter=',')
    data_n = data_r.copy()
    xsr = np.arange(data_n[sampling_num:, 0].shape[0])
    for i in range(sampling_num,data_r.shape[0]):
        min_data = np.min(data_r[i - sampling_num: i + 1,0])
        max_data = np.max(data_r[i - sampling_num: i + 1,0])
        data_n[i,0] = (data_r[i,0] - min_data)/(max_data-min_data)
    for i in range(sampling_num,data_r.shape[0]):
        min_data = np.min(data_r[i - sampling_num: i + 1,1])
        max_data = np.max(data_r[i - sampling_num: i + 1,1])
        data_n[i,1] = (data_r[i,1] - min_data)/(max_data-min_data)
    
    #原始数据
    plt.figure(0)
    plt.subplot(2, 1, 1)
    plt.title('x_position(Normalized)')
    plt.scatter(np.arange(len(data_n[sampling_num:boundary, 0]))+ sampling_num, data_n[sampling_num:boundary, 0], c = 'r', s = 7)
    plt.scatter(np.arange(boundary,boundary+len(data_n[boundary:, 0])), data_n[boundary:, 0], c = 'b', s = 7)
    plt.legend(labels=["spining","unspining"])
    
    plt.subplot(2, 1, 2)
    plt.title('z_position(Normalized)')
    plt.scatter(np.arange(len(data_n[sampling_num:boundary, 1])) + sampling_num, data_n[sampling_num:boundary, 1], c = 'r', s = 7)
    plt.scatter(np.arange(boundary,boundary+len(data_n[boundary:, 1])), data_n[boundary:, 1], c = 'b', s = 7)
    plt.legend(labels=["spining","unspining"])

    #频域
    datax1_fft = np.fft.fft(data_n[sampling_num:boundary, 0])
    datax2_fft = np.fft.fft(data_n[boundary:, 0])
    plt.figure(1)
    plt.subplot(2, 2, 1)
    plt.title('x_position_fft(Normalized)')
    plt.plot(np.arange(len(datax1_fft)), datax1_fft, c = 'r')
    plt.legend(labels=["spining","unspining"])
    plt.subplot(2, 2, 2)
    plt.plot(np.arange(len(datax2_fft)), datax2_fft, c = 'b')
    plt.legend(labels=["unspining"])

    dataz1_fft = np.fft.fft(data_n[sampling_num:boundary, 1])
    dataz2_fft = np.fft.fft(data_n[boundary:, 1])
    plt.subplot(2, 2, 3)
    plt.title('z_position_fft(Normalized)')
    plt.plot(np.arange(len(dataz1_fft)), dataz1_fft, c = 'r')
    plt.legend(labels=["spining"])
    plt.subplot(2, 2, 4)
    plt.plot(np.arange(len(dataz2_fft)), dataz2_fft, c = 'b')
    plt.legend(labels=["unspining"])

    data_v = data_r.copy()
    #一阶差分(先归一化后差分)
    for i in range(sampling_num,data_r.shape[0]):
        data_v[i,0] = data_r[i,0] - data_r[i-1,0]
        data_v[i,1] = data_r[i,1] - data_r[i-1,1]
    #原始数据
    plt.figure(2)
    plt.subplot(2, 1, 1)
    plt.title('x_vesolity(Normalized)')
    plt.scatter(np.arange(len(data_v[sampling_num:boundary, 0]))+ sampling_num, data_v[sampling_num:boundary, 0], c = 'r', s = 7)
    plt.scatter(np.arange(boundary,boundary+len(data_v[boundary:, 0])), data_v[boundary:, 0], c = 'b', s = 7)
    plt.legend(labels=["spining","unspining"])
    plt.subplot(2, 1, 2)
    plt.title('z_vesolity(Normalized)')
    plt.scatter(np.arange(len(data_v[sampling_num:boundary, 1]))+ sampling_num, data_v[sampling_num:boundary, 1], c = 'r', s = 7)
    plt.scatter(np.arange(boundary,boundary+len(data_v[boundary:, 1])), data_v[boundary:, 0], c = 'b', s = 7)
    plt.legend(labels=["spining","unspining"])
    plt.show()
